<?php

/**
 * 模板引擎调试
 * @author 暮雨秋晨
 * @copyright 2014
 */

class TemplateCheck
{
    private static $l_d = '{'; //左
    private static $r_d = '}'; //右
    private static $dir = ''; //模板目录

    public function __construct($l, $r, $dir)
    {
        self::$l_d = $l;
        self::$r_d = $r;
        self::$dir = $dir;
        return true;
    }

    public function syntax_chk($res)
    {
        $this->syntax_check($res);
    }

    /**
     * 语法检查
     */
    private function syntax_check($res)
    {
        $res = explode("\n", $res);
        $res = array_filter($res);
        if (empty($res)) {
            throw new TemplateException('This template file is empty.', 2, 0, self::$dir);
        }
        $this->chk_literal($res);
        $this->chk_if($res);
        $this->chk_for($res);
        $this->chk_foreach($res);
        $this->chk_switch($res);
        return true;
    }

    private function chk_literal($res)
    {
        $literal = 0;
        $endliteral = 0;
        $_literal = 0;
        $_endliteral = 0;
        foreach ($res as $key => $src) {
            if (preg_match('!' . self::$l_d . 'literal' . self::$r_d . '!Uis', $src)) {
                $literal++;
                $_literal = $key;
            }
            if (preg_match('!' . self::$l_d . '\/literal' . self::$r_d . '!Uis', $src)) {
                $endliteral++;
                $_endliteral = $key;
            }
        }
        if ($literal != $endliteral) {
            throw new TemplateException('Grammar <b>literal</b> no normally closed.', 2, $_literal,
                self::$dir);
        }
    }

    private function chk_foreach($res)
    {
        $foreach = 0;
        $endforeach = 0;
        $_foreach = 0;
        $_endforeach = 0;
        foreach ($res as $key => $src) {
            if (preg_match('!' . self::$l_d . 'foreach.*' . self::$r_d . '!Uis', $src)) {
                $foreach++;
                $_foreach = $key;
            }
            if (preg_match('!' . self::$l_d . '\/foreach' . self::$r_d . '!Uis', $src)) {
                $endforeach++;
                $_endforeach = $key;
            }
        }
        if ($foreach != $endforeach) {
            throw new TemplateException('Grammar <b>foreach</b> no normally closed.', 2, $_foreach,
                self::$dir);
        }
    }

    private function chk_switch($res)
    {
        $switch = 0;
        $endswitch = 0;
        $_switch = 0;
        $_endswitch = 0;
        foreach ($res as $key => $src) {
            if (preg_match('!' . self::$l_d . 'switch\(.*\)' . self::$r_d . '!Uis', $src)) {
                $switch++;
                $_switch = $key;
            }
            if (preg_match('!' . self::$l_d . '\/switch' . self::$r_d . '!Uis', $src)) {
                $endswitch++;
                $_endswitch = $key;
            }
        }
        if ($switch != $endswitch) {
            throw new TemplateException('Grammar <b>switch</b> no normally closed.', 2, $_switch,
                self::$dir);
        }
    }

    private function chk_for($res)
    {
        $for = 0;
        $endfor = 0;
        $_for = 0;
        $_endfor = 0;
        foreach ($res as $key => $src) {
            if (preg_match('!' . self::$l_d . 'for\(.*\)' . self::$r_d . '!Uis', $src)) {
                $for++;
                $_for = $key;
            }
            if (preg_match('!' . self::$l_d . '\/for' . self::$r_d . '!Uis', $src)) {
                $endfor++;
                $_endfor = $key;
            }
        }
        if ($for != $endfor) {
            throw new TemplateException('Grammar <b>for</b> no normally closed.', 2, $_for,
                self::$dir);
        }
    }

    private function chk_if($res)
    {
        $if = 0;
        $endif = 0;
        $_if = 0;
        $_endif = 0;
        foreach ($res as $key => $src) {
            if (preg_match('!' . self::$l_d . 'if\(.*\)' . self::$r_d . '!Uis', $src)) {
                $if++;
                $_if = $key;
            }
            if (preg_match('!' . self::$l_d . '\/if' . self::$r_d . '!Uis', $src)) {
                $endif++;
                $_endif = $key;
            }
        }
        if ($if != $endif) {
            throw new TemplateException('Grammar <b>if</b> no normally closed.', 2, $_if,
                self::$dir);
        }
    }

}

?>